home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / librw / RWBTreeOnDisk.z / RWBTreeOnDisk
Encoding:
Text File  |  2002-10-03  |  20.5 KB  |  463 lines

  1.  
  2.  
  3.  
  4. RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))                                        RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))
  5.  
  6.  
  7.  
  8. NNNNaaaammmmeeee
  9.      RWBTreeOnDisk - Rogue Wave library class
  10.  
  11. SSSSyyyynnnnooooppppssssiiiissss
  12.               typedef long RWstoredValue ;
  13.  
  14.  
  15.  
  16.               typedef int (*RWdiskTreeCompare)(const char*, const char*,
  17.                                            size_t);
  18.           #include <rw/disktree.h>
  19.           #include <rw/filemgr.h>
  20.           RWFileManager fm("filename.dat");
  21.           RWBTreeOnDisk bt(fm);
  22.  
  23.  
  24.  
  25.  
  26. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn
  27.      Class RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk represents an ordered collection of associations of
  28.      keys and values, where the ordering is determined by comparing keys using
  29.      an external function.  The user can set this function.  Duplicate keys
  30.      are not allowed.  Given a key, the corresponding value can be found.
  31.      This class is specifically designed for managing a B-tree in a disk file.
  32.      Keys, defined to be arrays of cccchhhhaaaarrrrssss, and values, defined by the typedef
  33.      RRRRWWWWssssttttoooorrrreeeeddddVVVVaaaalllluuuueeee, are stored and retrieved from a B-tree.  The values can
  34.      represent offsets to locations in a file where objects are stored. The
  35.      key length is set by the constructor.  By default, this value is 16
  36.      characters.  By default, keys are null-terminated.  However, the tree can
  37.      be used with embedded nulls, allowing multibyte and binary data to be
  38.      used as keys.  To do so you must:
  39.           Specify TTTTRRRRUUUUEEEE for parameter iiiiggggnnnnoooorrrreeeeNNNNuuuullllllll in the constructor (see
  40.           below);
  41.  
  42.           Make sure all buffers used for keys are at least as long as the key
  43.           length (remember, storage and comparison will nnnnooootttt stop with a null
  44.           value);
  45.  
  46.           Use a comparison function (such as mmmmeeeemmmmccccmmmmpppp(((())))) that ignores nulls.
  47.  
  48.      This class is meant to be used with class RRRRWWWWFFFFiiiilllleeeeMMMMaaaannnnaaaaggggeeeerrrr which manages the
  49.      allocation and deallocation of space in a disk file. When you construct
  50.      an RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk you give the location of the root node in the
  51.      constructor as argument ssssttttaaaarrrrtttt.  If this value is RRRRWWWWNNNNIIIILLLL (the default) then
  52.      the location will be retrieved from the RRRRWWWWFFFFiiiilllleeeeMMMMaaaannnnaaaaggggeeeerrrr using function
  53.      ssssttttaaaarrrrtttt(((()))) (see class RRRRWWWWFFFFiiiilllleeeeMMMMaaaannnnaaaaggggeeeerrrr).  You can also use the enumeration
  54.      ccccrrrreeeeaaaatttteeeeMMMMooooddddeeee to set whether to use an existing tree (creating one if one
  55.      doesn't exist) or to force the creation of a new tree.  The location of
  56.      the resultant root node can be retrieved using member function
  57.      bbbbaaaasssseeeeLLLLooooccccaaaattttiiiioooonnnn(((())))....  More than one B-tree can exist in a disk file.  Each
  58.      must have its own separate root node.  This can be done by constructing
  59.      more than one  RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk, each with ccccrrrreeeeaaaatttteeeeMMMMooooddddeeee set to ccccrrrreeeeaaaatttteeee.  The
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))                                        RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))
  71.  
  72.  
  73.  
  74.      oooorrrrddddeeeerrrr of the B-tree can be set in the constructor.  Larger values will
  75.      result in shallower trees, but less efficient use of disk space.  The
  76.      minimum number of entries in a node can also be set.  Smaller values may
  77.      result in less time spent balancing the tree, but less efficient use of
  78.      disk space.
  79.  
  80. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee
  81.      None
  82.  
  83. EEEEnnnnuuuummmmeeeerrrraaaattttiiiioooonnnnssss
  84.               enum styleMode {V6Style, V5Style};
  85.  
  86.  
  87.      This enumeration is used by the constructor to allow backwards
  88.      compatibility with older V5.X style trees, which supported only 16-byte
  89.      key lengths.  It is used only when creating a new tree.  If opening a
  90.      tree for update, its type is determined automatically at runtime.
  91.  
  92.      VVVV6666SSSSttttyyyylllleeee Initialize a new tree using V6.X style trees.  This is the
  93.      default.
  94.  
  95.      VVVV5555SSSSttttyyyylllleeee Initialize a new tree using V5.X style trees.  In this case, the
  96.      key length is fixed at 16 bytes.
  97.  
  98.               enum createMode {autoCreate, create};
  99.  
  100.  
  101.      This enumeration is used by the constructor to determine whether to force
  102.      the creation of a new tree.
  103.  
  104.      aaaauuuuttttooooCCCCrrrreeeeaaaatttteeee Look in the location given by the constructor argument ssssttttaaaarrrrtttt
  105.      for the root node.  If valid, use it.  Otherwise, allocate a new tree.
  106.      This is the default.
  107.  
  108.      ccccrrrreeeeaaaatttteeee Forces the creation of a new tree.  The argument ssssttttaaaarrrrtttt is ignored.
  109.  
  110. PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrr
  111.               RWBTreeOnDisk(RWFileManager& f,
  112.                         unsigned nbuf        = 10,
  113.                         createMode omode     = autoCreate,
  114.                         unsigned keylen      = 16,
  115.                         RWBoolean ignoreNull = FALSE,
  116.                         RWoffset start       = RWNIL,
  117.                         styleMode smode      = V6Style,
  118.                         unsigned halfOrder   = 10,
  119.                         unsigned minFill     = 10);
  120.  
  121.  
  122.      Construct a B-tree on disk.  The parameters are as follows:
  123.  
  124.      ffff The file in which the B-tree is to be managed.  This is the only
  125.      required parameter.
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))                                        RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))
  137.  
  138.  
  139.  
  140.      nnnnbbbbuuuuffff The maximum number of nodes that can be cached in memory.
  141.  
  142.      oooommmmooooddddeeee Determines whether to force the creation of a new tree or whether
  143.      to attempt to open an existing tree for update (the default).
  144.  
  145.      kkkkeeeeyyyylllleeeennnn The length of a key in bytes.  Ignored when opening an existing
  146.      tree.
  147.  
  148.      iiiiggggnnnnoooorrrreeeeNNNNuuuullllllll Controls whether to allow embedded nulls in keys.  If FFFFAAAALLLLSSSSEEEE
  149.      (the default), then keys end with a terminating null.  If TTTTRRRRUUUUEEEE, then all
  150.      kkkkeeeeyyyylllleeeennnn bytes are significant.  Ignored when opening an existing tree.
  151.  
  152.      ssssttttaaaarrrrtttt Where to find the root node.  If set to RRRRWWWWNNNNIIIILLLL (the default), then
  153.      uses the value returned by the RRRRWWWWFFFFiiiilllleeeeMMMMaaaannnnaaaaggggeeeerrrr''''ssss ssssttttaaaarrrrtttt(((()))) member function.
  154.      Ignored when creating a new tree.
  155.  
  156.      ssssmmmmooooddddeeee Sets the type of B-tree to create, allowing backwards compatibility
  157.      (see above).  The default specifies new V6.X style B-trees.  Ignored when
  158.      opening an existing tree.
  159.  
  160.      hhhhaaaallllffffOOOOrrrrddddeeeerrrr One half the order of the B-tree (that is, one half the number
  161.      of entries in a node).  Ignored when opening an existing tree.
  162.  
  163.      mmmmiiiinnnnFFFFiiiillllllll The minimum number of entries allowed in a node (must be less
  164.      than or equal to hhhhaaaallllffffOOOOrrrrddddeeeerrrr).  Ignored when opening an existing tree.
  165.  
  166. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnnssss
  167.               void
  168.           aaaappppppppllllyyyyTTTTooooKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee((*ap)(const char*,RWstoredValue), void* x);
  169.  
  170.  
  171.      Visits all items in the collection in order, from smallest to largest,
  172.      calling the user-provided function pointed to by aaaapppp with the key and
  173.      value as arguments.  This function should have the prototype:
  174.  
  175.               void yyyyoooouuuurrrrAAAAppppppppllllyyyyFFFFuuuunnnnccccttttiiiioooonnnn(const char* ky,
  176.  
  177.  
  178.  
  179.                                      RWstoredValue val,void* x);
  180.  
  181.  
  182.      The function yyyyoooouuuurrrrAAAAppppppppllllyyyyFFFFuuuunnnnccccttttiiiioooonnnn mmmmaaaayyyy nnnnooootttt change the key.  The value xxxx can
  183.      be anything and is passed through from the call to aaaappppppppllllyyyyTTTTooooKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(((()))).
  184.      This member function may throw an RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr exception.
  185.  
  186.               RWoffset
  187.           bbbbaaaasssseeeeLLLLooooccccaaaattttiiiioooonnnn() const;
  188.  
  189.  
  190.      Returns the offset of this tree's starting location within the
  191.      RRRRWWWWFFFFiiiilllleeeeMMMMaaaannnnaaaaggggeeeerrrr.  This is the value you will pass to a constructor as the
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))                                        RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))
  203.  
  204.  
  205.  
  206.      ssssttttaaaarrrrtttt argument when you want to open one of several trees stored in one
  207.      managed file.
  208.  
  209.               unsigned
  210.           ccccaaaacccchhhheeeeCCCCoooouuuunnnntttt() const;
  211.  
  212.  
  213.      Returns the maximum number of nodes that may currently be cached.
  214.  
  215.               unsigned
  216.           ccccaaaacccchhhheeeeCCCCoooouuuunnnntttt(unsigned newcount);
  217.  
  218.  
  219.      Sets the number of nodes that should be cached to nnnneeeewwwwccccoooouuuunnnntttt. Returns the
  220.      old number.
  221.  
  222.               void
  223.           cccclllleeeeaaaarrrr();
  224.  
  225.  
  226.      Removes all items from the collection.This member function may throw an
  227.      RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr exception.
  228.  
  229.               RWBoolean
  230.           ccccoooonnnnttttaaaaiiiinnnnssss(const char* ky) const;
  231.  
  232.  
  233.      Returns TTTTRRRRUUUUEEEE if the tree contains a key that is equal to the string
  234.      pointed to by kkkkyyyy, and FFFFAAAALLLLSSSSEEEE otherwise.  This member function may throw an
  235.      RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr exception.
  236.  
  237.               size_t
  238.           eeeennnnttttrrrriiiieeeessss();
  239.  
  240.  
  241.      Returns the number of items in the RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk. This member function
  242.      may throw an RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr exception.
  243.  
  244.  
  245.           RWoffset
  246.           eeeexxxxttttrrrraaaaLLLLooooccccaaaattttiiiioooonnnn(RWoffset newlocation);
  247.  
  248.  
  249.      Sets the location where this RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk keeps your own application-
  250.      specific information to nnnneeeewwwwllllooooccccaaaattttiiiioooonnnn.  Returns the previous value.
  251.  
  252.               RWBoolean
  253.           ffffiiiinnnnddddKKKKeeeeyyyy( const char* ky, RWCString& foundKy)const ;
  254.  
  255.  
  256.      Returns TTTTRRRRUUUUEEEE if kkkkyyyy  is found, otherwise FFFFAAAALLLLSSSSEEEE....  If successful, the found
  257.      key is returned as a reference in ffffoooouuuunnnnddddKKKKyyyy. This member function may throw
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))                                        RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))
  269.  
  270.  
  271.  
  272.      an RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr exception.
  273.  
  274.               RWBoolean
  275.           ffffiiiinnnnddddKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee( const char* ky,
  276.                            RWCString& foundKy,
  277.                            RWStoredValue& foundVal)const ;
  278.  
  279.  
  280.      Returns TTTTRRRRUUUUEEEE if kkkkyyyy  is found, otherwise FFFFAAAALLLLSSSSEEEE....  If successful, the found
  281.      key is returned as a reference in ffffoooouuuunnnnddddKKKKyyyy, and the value is returned as a
  282.      reference in ffffoooouuuunnnnddddVVVVaaaallll. This member function may throw an RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr
  283.      exception.
  284.  
  285.               RWstoredValue
  286.           ffffiiiinnnnddddVVVVaaaalllluuuueeee(const char* ky)const;
  287.  
  288.  
  289.      Returns the value for the key that compares equal to the string pointed
  290.      to by kkkkyyyy.  Returns RRRRWWWWNNNNIIIILLLL if no key is found. This member function may
  291.      throw an RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr exception.
  292.  
  293.               int
  294.           hhhheeeeiiiigggghhhhtttt();
  295.  
  296.  
  297.      Returns the height of the RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk.  A possible exception is
  298.      RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr.
  299.  
  300.               int
  301.           iiiinnnnsssseeeerrrrttttKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(const char* ky,RWstoredValue v);
  302.  
  303.  
  304.      Adds a key-value pair to the B-tree.  Returns TTTTRRRRUUUUEEEE for successful
  305.      insertion, FFFFAAAALLLLSSSSEEEE otherwise.  A possible exception is RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr.
  306.  
  307.               unsigned
  308.           kkkkeeeeyyyyLLLLeeeennnnggggtttthhhh() const;
  309.  
  310.  
  311.      Return the length of the keys for this RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk.  This number is set
  312.      when the tree is first constructed and cannot be changed.
  313.  
  314.               unsigned
  315.           mmmmiiiinnnnOOOOrrrrddddeeeerrrr()const;
  316.  
  317.  
  318.      Return the minimum number of items that may be found in any non-root node
  319.      in this RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk.  This number is set when the tree is first
  320.      constructed and cannot be changed.
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))                                        RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))
  335.  
  336.  
  337.  
  338.               unsigned
  339.           nnnnooooddddeeeeSSSSiiiizzzzeeee() const;
  340.  
  341.  
  342.      Returns the number of bytes used by each node of this RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk.
  343.      This number is calculated from the length of the keys and the order of
  344.      the tree, and cannot be changed.  We make it available to you for your
  345.      calculations about how many nodes to cache.
  346.  
  347.               unsigned
  348.           oooorrrrddddeeeerrrr()const;
  349.  
  350.  
  351.      Return half the maximum number of items that may be stored in any node in
  352.      this RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk.  This number is set when the tree is first
  353.      constructed and cannot be changed.  This method should have been renamed
  354.      "hhhhaaaallllffffOOOOrrrrddddeeeerrrr" but is still called "oooorrrrddddeeeerrrr" for backward compatibility.
  355.  
  356.               RWBoolean
  357.           iiiissssEEEEmmmmppppttttyyyy() const;
  358.  
  359.  
  360.      Returns TTTTRRRRUUUUEEEE if the RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk is empty, otherwise FFFFAAAALLLLSSSSEEEE.
  361.  
  362.               void
  363.           rrrreeeemmmmoooovvvveeee(const char* ky);
  364.  
  365.  
  366.      Removes the key and value pair that has a key which matches kkkkyyyy. This
  367.      member function may throw an RRRRWWWWFFFFiiiilllleeeeEEEErrrrrrrr exception.
  368.  
  369.  
  370.           RWBoolean
  371.           rrrreeeeppppllllaaaacccceeeeVVVVaaaalllluuuueeee(const RWCString& key,
  372.                        const RWstoredValue newval,
  373.                        RWstoredValue& oldVal);
  374.  
  375.  
  376.      Attempts to replace the RRRRWWWWssssttttoooorrrreeeeddddVVVVaaaalllluuuueeee now associated with kkkkeeeeyyyy by the
  377.      value nnnneeeewwwwvvvvaaaallll.  If successful, the previous value is returned by reference
  378.      in oooollllddddVVVVaaaallll; and the methed returns TTTTRRRRUUUUEEEE.  Otherwise, returns FFFFAAAALLLLSSSSEEEE.
  379.  
  380.               RWdiskTreeCompare
  381.           sssseeeettttCCCCoooommmmppppaaaarrrriiiissssoooonnnn(RWdiskTreeCompare fun);
  382.  
  383.  
  384.      Changes the comparison function to ffffuuuunnnn and returns the old function.
  385.      This function must have prototype:
  386.  
  387.                  int yyyyoooouuuurrrrFFFFuuuunnnn(const char* key1, const char* key2, size_t N);
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))                                        RRRRWWWWBBBBTTTTrrrreeeeeeeeOOOOnnnnDDDDiiiisssskkkk((((3333CCCC++++++++))))
  401.  
  402.  
  403.  
  404.  
  405.  
  406.      It should return a number less than zero, equal to zero, or greater than
  407.      zero depending on whether the first argument is less than, equal to or
  408.      greater than the second argument, respectively.  The third argument is
  409.      the key length.  Possible choices (among others) are ssssttttrrrrnnnnccccmmmmpppp(((()))) (the
  410.      default), or ssssttttrrrrnnnniiiiccccmmmmpppp(((()))) (for case-independent comparisons).
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.